﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetUtilityLib;
using db;
using System.Data.Common;
using DMSMT.MaterialMgr;
using System.Reflection;

namespace DMSMT
{
    public partial class MainFrm : Form
    {
        private Form childForm;
        private const int CP_NOCLOSE_BUTTON = 0x200;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }
        public MainFrm()
        {
            InitializeComponent();
        }

        private void ShowChild(Form frm)
        {
            foreach (Form form in this.MdiChildren)
            {
                form.Close();
            }
            frm.MdiParent = this;
            frm.ControlBox = false;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void ShowChild(string formClassName)
        {
            Form activeFrm = this.ActiveMdiChild;
            if (activeFrm != null)
            {
                if (activeFrm.GetType().FullName == formClassName)
                    return;
            }
            Form frm = (Form)Assembly.GetExecutingAssembly().CreateInstance(formClassName);
            this.ShowChild(frm);
        }

        private void ShowChildInPanel(string formClassName)
        {
            Form activeFrm = this.childForm;
            if (activeFrm != null)
            {
                if (activeFrm.GetType().FullName == formClassName)
                    return;
                else
                {
                    this.childForm.Close();
                    this.childForm.Dispose();
                }
            }
           
            Form frm = (Form)Assembly.GetExecutingAssembly().CreateInstance(formClassName);
            frm.TopLevel = false;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            this.pForm.Controls.Add(frm);
            //this.pForm.Controls.Clear();
            this.childForm = frm;
            this.rtbLog.Clear();
            frm.Show();
        }

        private void menu_ItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem mItem = (ToolStripMenuItem)sender;
            ClickMenuItem(mItem);
        }

        private void ClickMenuItem(ToolStripMenuItem mItem)
        {
            string name = mItem.Tag.ToString();
            this.ShowChildInPanel(name);
            sl.Text = mItem.Text;
            this.Text = "DMS运维问题工具 - " + mItem.Text;
        }

        private void BindMenuClick(ToolStripMenuItem mItem)
        {
            if (mItem.HasDropDownItems)
            {
                foreach (ToolStripMenuItem item in mItem.DropDownItems)
                {
                    BindMenuClick(item);
                }

            }
            else
            {
                if (mItem.Tag != null)
                    mItem.Click += new EventHandler(menu_ItemClick);
            }

        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem mItem in menuMain.Items)
            {
                BindMenuClick(mItem);
            }
            this.MinimumSize = new Size(1000,700);
            
            Log.SetInstance(this.rtbLog);
            //ClickMenuItem(tsmiFileCopyTool);
            //string info=file.CommonCmd.RunCmd("ipconfig /all");
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }      
    }
}
